package com.demo.boss.server.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.boss.common.constants.type.SysException;
import com.demo.boss.common.enums.PublicStatus;
import com.demo.boss.common.enums.SysCode;
import com.demo.boss.controller.PayWayController;
import com.demo.boss.entity.PayWay;
import com.demo.boss.mapper.PayWayMapper;
import com.demo.boss.models.PageRequest;
import com.demo.boss.models.request.PayWayConfigRequest;
import com.demo.boss.models.request.PayWayRequest;
import com.demo.boss.models.vo.DicItemVo;
import com.demo.boss.server.PayWayService;
import com.demo.boss.settings.BossConfiguration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PayWayServiceImpl implements PayWayService {
    @Resource
    private PayWayMapper payWayMapper;

    @Override
    public IPage<PayWay> pageList(String productCode, PageRequest pageRequest) {
        Integer pageNo = pageRequest.getPageNo();
        Integer pageSize = pageRequest.getPageSize();
        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("pay_product_code", "way_code", "type_code");
        if (StrUtil.isNotBlank(productCode)) {
            wrapper.eq("pay_product_code", productCode);
        }
        return payWayMapper.selectPage(Page.of(pageNo, pageSize), wrapper);
    }

    @Override
    public void removePayWay(PayWayRequest payWayRequest) {
        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        wrapper.eq("way_code", payWayRequest.getWayCode())
                .eq("type_code", payWayRequest.getTypeCode())
                .eq("pay_product_code", payWayRequest.getProductCode());
        int delete = payWayMapper.delete(wrapper);
        if (delete == 0) {
            throw new SysException(SysCode.DEFAULT_ERROR);
        }
    }

    @Override
    public List<DicItemVo> queryPayWayDic() {

        return BossConfiguration.dicPayWayDicList;
    }

    @Override
    public List<DicItemVo> queryPayTypeDic(String wayCode) {
        List<DicItemVo> dicItemVoList = BossConfiguration.dicPayTypeMap.get(wayCode);
        return dicItemVoList == null ? new ArrayList<>() : dicItemVoList;
    }

    /**
     * 创建支付产品，多个支付方式和类型  表 pay_way
     * Tip: 一个支付产品可以配置多个支付方式，多个支付类型。但支付产品+支付方式+支付类型 唯一的
     */
    @Override
    public void createPayWayConfig(PayWayConfigRequest payWayConfigRequest) {
        // 检查三者是否唯一
        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_product_code", payWayConfigRequest.getProductCode())
                .eq("way_code", payWayConfigRequest.getWayCode())
                .eq("type_code", payWayConfigRequest.getTypeCode());
        Long count = payWayMapper.selectCount(wrapper);
        if (count > 0) {
            throw new SysException(SysCode.PAYMENT_PRODUCT_WAY_EXISTS_ERROR);
        }
        // 到这里就可以进行添加了
        PayWay payWay = new PayWay();
        payWay.setId(IdWorker.get32UUID());
        payWay.setWayCode(payWayConfigRequest.getWayCode());
        payWay.setWayName(BossConfiguration.DIC_PAY_WAY_MAP.get(payWayConfigRequest.getWayCode()));
        payWay.setTypeCode(payWayConfigRequest.getTypeCode());
        payWay.setTypeName(BossConfiguration.DIC_PAY_TYPE_MAP.get(payWayConfigRequest.getTypeCode()));
        payWay.setPayProductCode(payWayConfigRequest.getProductCode());
        payWay.setPayRate(payWayConfigRequest.getPayRate());
        payWay.setSorts(payWayConfigRequest.getSorts());
        payWay.setStatus(PublicStatus.ENABLE.name());
        payWay.setEditTime(new Date());
        payWay.setRemark("创建支付产品配置记录");
        int insert = payWayMapper.insert(payWay);
        System.out.println(insert);
    }
}
